﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// Divide.xaml 的交互逻辑
    /// </summary>
    public partial class Divide : UserControl
    {
        public Divide()
        {
            InitializeComponent();
        }

        #region 依赖属性
        public object MiddleContent
        {
            get => GetValue(MiddleContentProperty);
            set => SetValue(MiddleContentProperty, value);
        }


        public static readonly DependencyProperty MiddleContentProperty =
            DependencyProperty.Register(nameof(MiddleContent), typeof(object), typeof(Divide), new PropertyMetadata(null,OnMiddleContentChanged));

        private static void OnMiddleContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Divide div)
            {
                if (e.NewValue == null)
                {
                    div.contentCtrl.Visibility = Visibility.Collapsed;
                }
                else
                {
                    div.contentCtrl.Visibility = Visibility.Visible;
                }
            }
        }

        public DivideAlignment ContentAlignment
        {
            get => (DivideAlignment)GetValue(ContentAlignmentProperty);
            set => SetValue(ContentAlignmentProperty, value);
        }

        public static readonly DependencyProperty ContentAlignmentProperty =
            DependencyProperty.Register(nameof(ContentAlignment), typeof(DivideAlignment), typeof(Divide),
             new PropertyMetadata(DivideAlignment.Center, OnVisualPropertyChanged));


        public double LineThickness
        {
            get => (double)GetValue(LineThicknessProperty);
            set => SetValue(LineThicknessProperty, value);
        }

        public static readonly DependencyProperty LineThicknessProperty =
            DependencyProperty.Register("LineThickness", typeof(double), typeof(Divide), new PropertyMetadata(1.0));


        public Brush LineBrush
        {
            get => (Brush)GetValue(LineBrushProperty);
            set => SetValue(LineBrushProperty, value);
        }

        public static readonly DependencyProperty LineBrushProperty =
            DependencyProperty.Register("LineBrush", typeof(Brush), typeof(Divide), new PropertyMetadata(Brushes.LightGray));
        #endregion


        #region 依赖方法
        private static void OnVisualPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Divide)?.UpdateVisual();
        }

        private void UpdateVisual()
        {
            // 横向布局调整
            switch (ContentAlignment)
            {
                case DivideAlignment.Left:
                    GridRoot.ColumnDefinitions[0].Width = new GridLength(0.1, GridUnitType.Star);
                    GridRoot.ColumnDefinitions[2].Width = new GridLength(0.9, GridUnitType.Star);
                    break;
                case DivideAlignment.Right:
                    GridRoot.ColumnDefinitions[0].Width = new GridLength(0.9, GridUnitType.Star);
                    GridRoot.ColumnDefinitions[2].Width = new GridLength(0.1, GridUnitType.Star);
                    break;
                default:
                    GridRoot.ColumnDefinitions[0].Width = new GridLength(0.5, GridUnitType.Star);
                    GridRoot.ColumnDefinitions[2].Width = new GridLength(0.5, GridUnitType.Star);
                    break;
            }
        }

        #endregion
    }

    public enum DivideAlignment
    {
        Left,
        Center,
        Right
    }

}
